package com.frxuexi.park2303.notice

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.frxuexi.base.http.ApiService
import com.frxuexi.base.mvi.BaseViewModel
import com.frxuexi.base.mvi.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class NoticeViewModel @Inject constructor(val apiService: ApiService):BaseViewModel<NoticeIntent>() {

    override fun handleIntent(it: NoticeIntent) {
        when(it){
            is NoticeIntent.getNotices->{
                pagerRequest { NoticeDataSource(apiService) }
            }
            is NoticeIntent.addNotice->httpRequest(apiService.addNotice(toBody(it.item)))
            is NoticeIntent.getNoticeByUid->httpRequest(apiService.getNoticeByUid(it.uid))
            is NoticeIntent.getNoticeById->httpRequest(apiService.getNoticeById(it.id))
        }
    }
}